<?php 
class UserModel extends Model{
	public $table="user";
	/**
	 * 注册
	 */
	 public function register(){
	 	if(!$this->create()) return false;
		if(IS_POST){			
			//判断验证码
			if(strtoupper($_POST['code1'])!=$_SESSION['code']){
				$this->error="验证码错误";
				return false;
            }
			//判断用户名是否为空
			if($_POST['username']==""){
				$this->error="用户名不能为空";	
				return false;			
			}
			$username=$_POST['username'];
			$password=md5($_POST['password_']);
			//判断用户名是否已经被注册
			$check=M("user")->where("username='{$username}'")->find();
			if($check){
				$this->error="该邮箱已被注册";
				return false;
			}
			//判断密码
			if($_POST['password_']!=$_POST['password_new1']){
				$this->error="两次密码不一样";
				return false;
			}
			$this->data['password']=$password;
		}
		   return $this->add();
	 }
	 /**
	  * 前台登陆
	  */
	  public function login(){
	  	if(!$this->create()) return false;
		if(IS_POST){
			$username=Q("post.username");
			$password=Q("post.password","","md5");
			$data=M('user')->where("username='{$username}'")->find();
			if(!$data){
				$this->error="用户名不存在";
				return false;
			}
			if($data['password']!="$password"){
				$this->error="密码错误";
				return false;
				
			}
			
			session('uid',$data['uid']);
		    session('uname',$data['username']);
		}
         return true;
	  	
	  }
	 /**
	  * 后台登陆
	  */
	 public function login_admin(){
	  	if(!$this->create()) return false;
		if(IS_POST){
			$code=Q("post.verify","","strtoupper");	
			$scode=$_SESSION['code'];
			
			if($code!=$scode){
				$this->error="验证码错误";
				return false;
			}
			$username=Q("post.username");
			$password=Q("post.password","","md5");
			$data=M('user')->where("username='{$username}'")->find();
			if(!$data){
				$this->error="用户名不存在";
				return false;
			}
			if($data['password']!="$password"){
				$this->error="密码错误";
				return false;
				
			}
			
			session('aid',$data['uid']);
		    session('aname',$data['username']);
		}
         return true;
	  	
	  }
	 
	 
	 
	 
	 
	 
	 
	 
}


 ?>